* ======================================
* --------- Summary Efficiency ---------
* ======================================


* Efficiency for families
* =======================

	use $dta\Population.dta, clear
	keep idPers year knr
	keep if year == 2015
	rename knr knrPers

	foreach net in Fam1 Fam2{
	
		preserve	
		
		* Match in family
		merge 1:m idPers using $dta\Network`net'.dta, keep(matched) nogen ///
			keepusing(id`net')
	
		* Match in knr for family
		rename idPers tmp_idPers
		rename id`net' idPers
		merge m:1 idPers year using $dta\ImportGeo.dta, keep(matched) nogen ///
			keepusing(knr)
		rename idPers id`net'
		rename tmp_idPers idPers
		rename knr knr`net'
	
		* Compute efficiency
		gen n`net' = 1								// n members in each knr
		gen n`net'_ins = 1*(knrPers == knr`net')	// n members overall
		collapse (sum) n`net'*, by(idPers)
		gen eff`net' = n`net'_ins/n`net' // fraction inside

		keep idPers eff`net' n`net'
		tempfile `net'
		save ``net''
		
		restore
		}
	
	
* Efficiency for co-workers
* =========================

	use $dta\Population.dta, clear
	keep idPers year knr
	keep if year == 2015
	rename knr knrPers
	
	foreach net in Work1 Work2{
	
		preserve
		
		* Match networks
		merge m:1 idPers using $dta\Network`net'.dta, keep(matched) nogen ///
			keepusing(`net'net)
	
		* Compute efficiency
		bysort `net'net knr: gen n`net'_ins = _N	// n members in each knr
		bysort `net'net: gen n`net' = _N			// n members overall
		collapse n`net'*, by(`net'net)
		gen eff`net' = n`net'_ins/n`net'			// fraction inside
	
		keep `net'net eff`net' n`net'
		tempfile `net'
		save ``net''	
		
		restore
	}
		

* Efficiency for immigrants
* =========================

	use $dta\Population.dta, clear
	keep idPers year knr
	keep if year == 2015
	rename knr knrPers

	foreach net in Imm1 Imm2{
	
		preserve
	
		* Match networks
		merge m:1 idPers using $dta\Network`net'.dta, keep(matched) nogen ///
			keepusing(`net'net)
	
		* Compute efficiency
		bysort `net'net knr: gen n`net'_ins = _N	// n members in each knr
		bysort `net'net: gen n`net' = _N			// n members overall
		collapse n`net'*, by(`net'net)
		gen eff`net' = n`net'_ins/n`net'			// fraction inside
	
		keep `net'net eff`net' n`net'
		tempfile `net'
		save ``net''
		
		restore
	}
		
	
* Combine
* =======

	clear
	append using `Fam1'
	append using `Fam2'
	append using `Work1'
	append using `Work2'
	append using `Imm1'
	append using `Imm2'
	

* Summarize efficiency
* ====================

	foreach net in Fam1 Fam2 Work1 Work2 Imm1 Imm2{
		preserve
		keep if n`net' != . 
		
		sum eff`net'
		local n_`net' = string(r(N), "%9.0fc")
		local m_`net' = string(r(mean)*100, "%9.1fc")
		local sd_`net' = string(r(sd)*100, "%9.1fc")
		local min_`net' = string(r(min)*100, "%9.1fc")
		local max_`net' = string(r(max)*100, "%9.1fc")
		restore
	}
	
	
* Build table
* ===========
	local heading "\\ & \multicolumn{2}{c}{Family} & \multicolumn{2}{c}{Co-workers} & \multicolumn{2}{c}{Immigrants} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}" 

	local subhead " & Close  & Extended & Age-estbl. & Estbl. & 3-digit & 2-digit \\ \hline"
	local r1 " Mean & `m_Fam1' & `m_Fam2' & `m_Work1' & `m_Work2' & `m_Imm1' & `m_Imm2' \\"
	local r2 " Standard dev. & `sd_Fam1' & `sd_Fam2' & `sd_Work1' & `sd_Work2' & `sd_Imm1' & `sd_Imm2' \\"
	local r3 " Minimum &`min_Fam1' & `min_Fam2' & `min_Work1' & `min_Work2' & `min_Imm1' & `min_Imm2' \\"
	local r4 " Maximum &`max_Fam1' & `max_Fam2' & `max_Work1' & `max_Work2' & `max_Imm1' & `max_Imm2' \\ \hline"
	local r5 " N & `n_Fam1' & `n_Fam2' & `n_Work1' & `n_Work2' & `n_Imm1' & `n_Imm2' \\"

	
* Write table
* ===========

	capture file close efficiency

	file open efficiency using "$tbls\SummaryEfficiency.tex", write replace
	file write efficiency "\begin{tabular}{lcccccc} \hline\hline" _n
	file write efficiency "`heading'" _n
	file write efficiency "`subhead'" _n
	file write efficiency "`r1'" _n
	file write efficiency "`r2'" _n
	file write efficiency "`r3'" _n
	file write efficiency "`r4'" _n
	file write efficiency "`r5'" _n
	file write efficiency "\hline \hline \\" _n
	file write efficiency "\end{tabular}"
	file close efficiency

